package net.xju_9_10.mapper;

import net.xju_9_10.entity.Student;
import net.xju_9_10.entity.StudentCard;
import net.xju_9_10.entity.vo.ClazzVo;
import net.xju_9_10.entity.vo.StudentVo;
import net.xju_9_10.entity.vo.TeacherVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
@CacheNamespace
public interface StudentMapper {

    @Select("  SELECT *,card_id cardId,clazz_id clazzId FROM `student`")
    List<Student> findAll();

    @Delete(" delete from student where id = #{aaa}")
    Integer delete(Integer id);

    List<Student> selectBySnAndName(@Param("name") String name,@Param("sn") String sn);

    List<Student> selectBySnAndNameMap(Map<String, Object> map);

    Student selectById(int id);

    @MapKey("name")
    Map<String, Object> selectMap(String name);

//    @Select(" SELECT s.*,c.* FROM `student` s,card c WHERE s.card_id = c.id  AND s.id =#{id}")
//    @Results({
//            @Result(property = "id", column = "id", id = true),
//            @Result(property = "sn", column = "sn"),
//            @Result(property = "name", column = "name"),
//            @Result(property = "clazzId", column = "clazz_id"),
//            @Result(property = "cardId", column = "card_id"),
//            @Result(property = "studentCard.id", column = "id"),
//            @Result(property = "studentCard.balance", column = "balance"),
//    })

    @Select("select * from student where id =#{id}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "sn", column = "sn"),
            @Result(property = "name", column = "name"),
            @Result(property = "clazzId", column = "clazz_id"),
            @Result(property = "cardId", column = "card_id"),
            @Result(property = "studentCard",
                    column = "card_id",
                    javaType = StudentCard.class,
                    one = @One(select = "net.xju_9_10.mapper.StudentMapper.selectCardById"))
    })
    StudentVo selectStuCardById(int id);

    @Select("select * from card where id = #{id}" )
    StudentCard  selectCardById(int id);

    @Select("select  * from clazz where id = #{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "clazzName",column = "clazz_name"),
            @Result(property = "sum",column = "sum"),
            @Result(property = "studentList",
            column = "id",
            javaType = List.class,
            many = @Many(select = "net.xju_9_10.mapper.StudentMapper.selectByClazzId"))
    })
    ClazzVo selectClazzVo(int id);
    @Select("select * from student where clazz_id = #{id}")
    List<Student>  selectByClazzId(int id);

    List<TeacherVo> selectTeacherVo();

    List<Student> selectList(Student student);

    Integer insert(Student student);

    Integer update(Student student);

    @Select("<script>"+
            " select * from student where id in " +
            "        <foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>"+
            "</script>")
    List<Student> selectListByIds(@Param("ids") List<Integer> ids);

    Integer batchInsert(List<Student> students);
}
